jQuery(function($) {

    $('.selectJiraUserAnchor').click(function(event) {
        var anchorId = event.target.id;
        acproUserKeyKeeper = anchorId.substring("selectJiraUserAnchor".length);
        $.getJSON(jiraUsersPickerUrl, function(data) {
            dataKeeper = data;
            buildUsersDialog(function(user) {
                $("#tdJiraUserName" + acproUserKeyKeeper).text(user.userName);
                $("#tdJiraEmail" + acproUserKeyKeeper).text(user.email);
                $("#tdJiraFullName" + acproUserKeyKeeper).text(user.fullName);

                $("#userMapping" + acproUserKeyKeeper).val(user.userName);
            });
            $('#usersDialog').dialog('open');
        });
        return false;
    });

    $('.notifyCCSelectJiraUserAnchor').click(function(event) {
        var anchorId = event.target.id;
        notifyCCUserIdKeeper = anchorId.substring("notifyCCSelectJiraUserAnchor".length);
        $.getJSON(jiraUsersPickerUrl, function(data) {
            dataKeeper = data;
            buildUsersDialog(function(user) {
                $("#notifyCCtdJiraUserName" + notifyCCUserIdKeeper).text(user.userName);
                $("#notifyCCtdJiraEmail" + notifyCCUserIdKeeper).text(user.email);
                $("#notifyCCtdJiraFullName" + notifyCCUserIdKeeper).text(user.fullName);

                $("#notifyCCMapping" + notifyCCUserIdKeeper).val(user.userName);
            });
            $('#usersDialog').dialog('open');
        });
        return false;
    });

    var acproUserKeyKeeper;
    var notifyCCUserIdKeeper;
    var dataKeeper;

    function buildUsersDialog(func) {
        $("#usersDialogTblUsers tr:not(:first)").remove();
        var content = "";
        for (var i = 0; i < dataKeeper.users.length; i++) {
            var user = dataKeeper.users[i];
            content += "<tr>";
            content += "<td><a href='#' class='anchorJiraUserName' id='anchorJiraUserName" + i + "'>" + user.userName + "</a></td>";
            content += "<td>" + user.email + "</td>";
            content += "<td>" + user.fullName + "</td>";
            content += "</tr>";
        }
        $("#usersDialogTblUsers").append(content);
        $('.anchorJiraUserName').click(function(event) {
            var anchorId = event.target.id;
            var index = anchorId.substring("anchorJiraUserName".length);
            var user = dataKeeper.users[index];

            func(user);

            $('#usersDialog').dialog('close');
            return false;
        });
    }

    var usersDialogOpts = {
        position: ['center', 'middle'],
        autoOpen: false,
        height: 500,
        width: 500,
        resizable: false,
        modal: true,
        closeText: '',
        buttons: {
            'Cancel': function() {
                $('#usersDialog').dialog('close');
             }
        }
    };

    $('#usersDialog').dialog(usersDialogOpts);

});